package dialog;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import window.CollieModelPanel;
import window.ColliePropertyPanel;
import component.DataPanel;
import diagram.NodeElement;

public class InstanceAction extends JDialog{

	private DataPanel dataPanel;
	private NodeElement nodElement;
	
	public InstanceAction(Frame owner, NodeElement nodeElement) {
		super(owner,"Edit Instance",true);
		// on instancie les contenues de Name et Class 
		setSize(200, 200);
		JPanel contentPane = new JPanel(new BorderLayout());
		setContentPane(contentPane);
		JButton cancel = new JButton("cancel");
		JButton valider = new JButton("valider");

		cancel.addActionListener(new CancelListener());
		
		valider.addActionListener(new ValiderListener());
		
		JToolBar toolbar = new JToolBar();
		toolbar.add(cancel);
		toolbar.add(valider);
		
		
		getContentPane().add(toolbar,BorderLayout.SOUTH);
		
		dataPanel = new DataPanel();
		dataPanel.setNameInstance(nodeElement.getName());
		dataPanel.setClassInstance(nodeElement.getClassifier());
			
		nodElement = nodeElement;

		getContentPane().add(dataPanel,BorderLayout.CENTER);
		
	}

	class CancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
				InstanceAction.this.setVisible(false);
				InstanceAction.this.dispose();
		}
		
	}

	class ValiderListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
				
				//récupération du nom de l'instance 
	
				nodElement.setName(dataPanel.getNameInstance());  
				nodElement.setClassifier(dataPanel.getClassInstance());  
								
				InstanceAction.this.setVisible(false);
				InstanceAction.this.dispose();
				
				CollieModelPanel.getCollieModelPanel().repaint();
				
				ColliePropertyPanel.getColliePropertyPanel().updateAndRepaint();
		}
		
	}
}


